package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Answer;
import cn.wolfcode.luowowo.article.mapper.AnswerMapper;
import cn.wolfcode.luowowo.article.query.AnswerQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService {

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public Answer selectByPrimaryKey(Long id) {
        Answer answer = answerMapper.selectByPrimaryKey(id);
        return answer;
    }

    @Override
    public List<Answer> selectAll() {
        List<Answer> answers = answerMapper.selectAll();
        return answers;
    }

    @Override
    public Long saveOrUpdate(Answer answer) {
        if(answer.getContent().length()>30){
            answer.setSummary(answer.getContent().substring(0,30));
        }else{
            answer.setSummary(answer.getContent());
        }
        answer.setCreateTime(new Date());
        answer.setReleaseTime(new Date());
        answer.setLastUpdateTime(new Date());
        answerMapper.insert(answer);
        return null;
    }

    @Override
    public PageInfo<Answer> query(AnswerQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Answer> answers = answerMapper.query(qo);
        return new PageInfo<>(answers);
    }

    @Override
    public List<Answer> queryByViewTop3(Long id) {
        return null;
    }

    @Override
    public void addThumbup(Long answerId, int num) {
        answerMapper.addThumbup(answerId,num);
    }

    @Override
    public List<HashMap<String, Object>> replynumTop() {
        return answerMapper.replynumTop();
    }

    @Override
    public List<HashMap<String, Object>> thumbsupnumTop() {
        return answerMapper.thumbsupnumTop();
    }
}
